package com.net.framework.help.utils;

import android.media.MediaPlayer;

/**
 * Author：xiaobin.zhang
 * Email：154722566@qq.com
 * Date：2017-12-09 16:57
 * Desc：<--播放音频-->
 */
public class MediaPlayerUtil {

    private MediaPlayer mp;
    private boolean isPause;//是否为暂停状态


    public boolean isPause() {
        return isPause;
    }

    public MediaPlayer getMp() {
        return mp;
    }

    public void setMp(MediaPlayer mp) {
        this.mp = mp;
    }

    public MediaPlayerUtil() {
        mp = new MediaPlayer();
    }

    /**
     * 准备播放的音频
     *
     * @param url
     */
    public void prepare(String url) {
        try {
            isPause = false;
            mp.reset();
            mp.setDataSource(url);
            mp.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /***
     * 开始播放
     */
    public void playStart() {
        isPause = false;
        if (mp.isPlaying()) {
            mp.pause();
        } else {
            mp.start();
        }
    }

    /**
     * 暂停播放
     */
    public void playPause() {
        if (mp.isPlaying()) {
            mp.pause();
            isPause = true;
        }
    }

    /**
     * 停止播放
     */
    public void playStop() {
        if (mp != null) {
            mp.stop();
            try {
                mp.prepare();
                mp.seekTo(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
